package com.zw.pm.module.overview.program;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.gen.entity.Comment;
import com.zw.pm.gen.entity.Program;
import com.zw.pm.gen.entity.ProgramDoc;
import com.zw.pm.module.system.log.LogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description: 会议 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/overview/program")
public class ProgramController extends BaseController {

    @Autowired
    private ProgramService programService;
    @Autowired
    private LogService logService;

    /**
     * 会议列表
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"overview:program"})
    public String program(Model model) {
        model.addAttribute("categories", programService.findCategories());
        return "overview/program/page";
    }

    /**
     * 会议列表
     * @param program 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"overview:program"})
    public JsonResult list(ProgramModel program) {
        startPage();
        List<ProgramModel> list = programService.findAll(program);
        return jsonResult(list);
    }

    /**
     * 创建会议
     * @param program program
     * @return json
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"program:save"})
    public JsonResult saveProgram(Program program, @RequestParam("file") MultipartFile[] file) {
        logService.saveLog("新建会议", program.getTitle());
        return jsonResult(programService.saveProgram(program, file));
    }

    /**
     * 删除会议
     * @param program program
     * @return json
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions(value = {"program:save","program:reject"},logical = Logical.OR)
    public JsonResult delProgram(Program program) {
        ProgramModel p = programService.getById(program.getId());
        logService.saveLog("删除会议", p.getTitle());
        return jsonResult(programService.updateProgram(program, null));
    }

    /**
     * 提交会议
     * @param program program
     * @return json
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"program:commit"})
    public JsonResult commitProgram(Program program) {
        ProgramModel p = programService.getById(program.getId());
        logService.saveLog("提交会议", p.getTitle());
        return jsonResult(programService.updateProgram(program, null));
    }

    /**
     * 退回会议
     * @param program program
     * @return json
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"program:reject"})
    public JsonResult rejectProgram(Program program) {
        ProgramModel p = programService.getById(program.getId());
        logService.saveLog("退回会议", p.getTitle());
        return jsonResult(programService.updateProgram(program, null));
    }

    /**
     * 根据ID查询
     * @param id id
     * @return Program
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"program:update"})
    public ProgramModel id(Integer id) {
        return programService.getById(id);
    }

    /**
     * 修改会议
     * @param program program
     * @return json
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"program:update"})
    public JsonResult updateProgram(Program program, @RequestParam("file") MultipartFile[] file) {
        ProgramModel p = programService.getById(program.getId());
        logService.saveLog("修改会议", p, program);
        return jsonResult(programService.updateProgram(program, file));
    }

    /**
     * 删除项目文件
     *
     * @param doc doc
     * @return json
     */
    @ResponseBody
    @PostMapping("/file/del")
    @RequiresPermissions({"program:update"})
    public JsonResult delProject(ProgramDoc doc) {
        ProgramDoc d = programService.getFileById(doc.getId());
        logService.saveLog("删除文件", d.getName());
        return jsonResult(programService.updateDoc(doc));
    }

    /**
     * 会议页面
     * @param id id
     * @param model model
     * @return 页面路径
     */
    @GetMapping("/{id}")
    @RequiresPermissions({"overview:program"})
    public String detail(@PathVariable("id")Integer id, Model model) {
        ProgramModel program = programService.getById(id);
        List<CommentModel> comments = programService.findCommentByProgramId(id);
        model.addAttribute("program", program);
        model.addAttribute("comments", comments);
        return "overview/program/detail";
    }

    /**
     * 评价会议
     * @param comment comment
     * @return json
     */
    @ResponseBody
    @PostMapping("/comment")
    @RequiresPermissions({"comment:create"})
    public JsonResult comment(Comment comment) {
        return jsonResult(programService.saveComment(comment));
    }

    /**
     * 删除用户评价
     * @param id id
     * @return json
     */
    @ResponseBody
    @PostMapping("/comment/del")
    @RequiresPermissions({"comment:del"})
    public JsonResult delComment(Integer id) {
        return jsonResult(programService.delComment(id));
    }

}
